# Maintainer: Bjoern Lemke <lemke@lemke-it.com>

_realname=lfcbase
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.16.0
pkgrel=3
pkgdesc="LFC C++ base classes (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
url='http://www.lemke-it.com/'
license=('GPL3')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git"
         "${MINGW_PACKAGE_PREFIX}-ncurses")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc" "${MINGW_PACKAGE_PREFIX}-autotools")
source=("http://www.lemke-it.com/${_realname}-${pkgver}.tar.gz")
sha256sums=("9ffa9f2ba2fbd4f17234e7055eaa8ccdc771ca1c202d7407e4322fa84ea6a351")

prepare() {
  # for mingw build, we have to patch the tap-driver to filter out windows carrige returns from the test program output
  # yes, this is an ugly patch
  cat $srcdir/${_realname}-${pkgver}/build-aux/tap-driver.sh  | sed "s/LC_ALL\=C/sed \'s\/\\\\r\/\/g\'| LC_ALL=C/" > $srcdir/${_realname}-${pkgver}/build-aux/tap-driver-patch.sh 
  mv $srcdir/${_realname}-${pkgver}/build-aux/tap-driver-patch.sh $srcdir/${_realname}-${pkgver}/build-aux/tap-driver.sh 
  cd $srcdir/${_realname}-${pkgver}
}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  export CXXFLAGS="-DLFCNOCURSES -DDISABLE_SYSLOG -std=gnu++14"

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-static \
    --enable-shared

  make

  if [[ "${MINGW_PACKAGE_PREFIX}" != *-clang-* ]]; then
    cxx_lib="stdc++"
  else
    cxx_lib="c++"
  fi

  ${MINGW_PREFIX}/bin/cc -shared \
    -Wl,--out-implib,liblfcbase.dll.a \
    -o liblfcbase-0.dll \
    ${srcdir}/build-${MSYSTEM}/src/*.o \
    -lsystre -lws2_32 -lpthread -l${cxx_lib}
}

check() {
  cd "${srcdir}"/build-${MSYSTEM}
  make check
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  make install DESTDIR="${pkgdir}"

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/{bin,lib}
  install -m 0755 liblfcbase-0.dll "${pkgdir}${MINGW_PREFIX}/bin"
  install -m 0644 liblfcbase.dll.a "${pkgdir}${MINGW_PREFIX}/lib"
}
